package com.grantha.controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;

import com.grantha.bean.Book;
import com.grantha.util.DB;
import com.grantha.util.StartupConstants;
import com.grantha.util.Util;
import java.sql.*;

public class AddBooksServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	Logger log = Logger.getLogger(this.getClass());

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();

		log.info("Servlet Stated");

		String date = request.getParameter("date");
		String bookno = request.getParameter("accno");

		String author = request.getParameter("author");
		String title = request.getParameter("title");
		String publisher = request.getParameter("pubname");
		String isbn = request.getParameter("isbn");
		String subject = request.getParameter("subject");
		String classno = request.getParameter("classNo");
		String edition = request.getParameter("edition");
		// String date = request.getParameter("status");
		String year = request.getParameter("year");
		String pages = request.getParameter("pages");
		String cost = request.getParameter("cost");
		String volume = request.getParameter("volume");
		String source = request.getParameter("source");
		String billno = request.getParameter("billno");
		String moreinfo = request.getParameter("addi");

		log.info("bookno=" + bookno);
		log.info("date=" + date);
		log.info("year=" + year);
		log.info("cost=" + cost);

		//create new book object
		Book b = new Book(date, null, null, null, author, "", title, publisher,
				isbn, subject, edition, year, cost, source, classno, pages,
				volume, billno, moreinfo, null, "");

		//add book to library
		JSONObject output = StartupConstants.getLibrary().addBook(b);

		out.write(output.toString());

		log.error("Servlet ends");
	}
}